import mimetypes, datetime
from django.contrib.syndication.feeds import Feed, FeedDoesNotExist
from django.contrib.sites.models import Site
from podcasts.models import Podcast

class PodcastFeed(Feed):

	def get_object(self, bits):
		if len(bits) != 1:
			raise FeedDoesNotExist
		try:
			return Podcast.on_site.public().get(slug__exact=bits[0])
		except Podcast.DoesNotExist:
			raise FeedDoesNotExist

	
	#######################################
	# Meta data about the Podcast series
	
	def title(self, podcast):
		return podcast.title

	def description(self, podcast):
		return podcast.description

	def categories(self, podcast):
		return podcast.category.split(',')

	def link(self, podcast):
		return podcast.link or podcast.get_absolute_url()
		
	def author_name(self, podcast):
		# TODO: Not working.
		return podcast.author

	def copyright(self, podcast):
		# TODO: Not working.
		return podcast.copyright

	def items(self, podcast):
		# TODO: .published()[:10]
		return podcast.episodes
	
	
	#######################################
	# Details about the individual episode
	
	def item_author_name(self, episode):
		return episode.author_name

	def item_author_email(self, episode):
		return episode.author_email
	
	def item_author_link(self, episode):
		return episode.author_link
	
	def item_enclosure_url(self, episode):
		return u'http://%s%s' % (Site.objects.get_current().domain, episode.get_audio_file_url())
		
	def item_enclosure_length(self, episode):
		return episode.get_audio_file_size()
		
	def item_enclosure_mime_type(self, episode):
		mime = mimetypes.guess_type(episode.get_audio_file_filename())[0]
		if mime is None:
			mime = 'audio/mpeg'
		return mime
		
	def item_pubdate(self, episode):
		return episode.date
		
	def item_categories(self, episode):
		return episode.category.split(', ')
		
	def item_copyright(self, episode):
		return  episode.copyright